package com.os.web.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @Author Matt
 * @Date 2022-01-17
 * @Desc:
 * @Version V1.0
 */
@Controller
public class UploadController {

    //使用multipartFile接收上传的文件
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(MultipartFile file){
        try {
            //使用FileUtils快速写文件到磁盘
            FileUtils.writeByteArrayToFile(new File("E:/upload/" + file.getOriginalFilename()), file.getBytes());
            return "ok";
        }catch (Exception e){
            e.printStackTrace();
            return "wrong";
        }
    }

}
